#include "aboutdialog.h"
#include "mynotepad.h"
#include "ui_mynotepad.h"
#include <QMessageBox>
#include <QDebug>
#include <QFileDialog>
#include <QDir>
#include <QFile>
#include <QTextStream>
#include <QColorDialog>
#include <QFontDialog>
#include <QDesktopServices>
#include <QUrl>
#include <QDateTime>

MyNotepad::MyNotepad(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MyNotepad)
{
    ui->setupUi(this);
    ui->textEdit->setDocumentTitle("untitled -- notepad");
    setWindowTitle(ui->textEdit->documentTitle());

    connect(ui->newFileAction, &QAction::triggered, this, &MyNotepad::newFile);
    connect(ui->openFileAction, &QAction::triggered, this, &MyNotepad::openFile);
    connect(ui->saveFileAction, &QAction::triggered, this, &MyNotepad::saveFile);
    connect(ui->exitNotepadAction, &QAction::triggered, this, &MyNotepad::exitNotepad);
    connect(ui->saveAsFileAction, &QAction::triggered, this, &MyNotepad::saveAsFile);
    connect(ui->fontAction, &QAction::triggered, this, &MyNotepad::selectFont);
    connect(ui->colorAction, &QAction::triggered, this, &MyNotepad::selectColor);
    connect(ui->aboutAction, &QAction::triggered, this, &MyNotepad::doForAboutAction);

    connect(ui->getHelpAction, &QAction::triggered, this, &MyNotepad::seeHelp);
    connect(ui->dateAction, &QAction::triggered, this, &MyNotepad::setDateTime);

    connect(ui->copyAction, &QAction::triggered, ui->textEdit, &QTextEdit::copy);
    connect(ui->cutAction, &QAction::triggered, ui->textEdit, &QTextEdit::cut);
    connect(ui->pasteAction, &QAction::triggered, ui->textEdit, &QTextEdit::paste);
    connect(ui->undoAction, &QAction::triggered, ui->textEdit, &QTextEdit::undo);
    connect(ui->selectAllAction, &QAction::triggered, ui->textEdit, &QTextEdit::selectAll);

}

MyNotepad::~MyNotepad()
{
    delete ui;
}

//新建文件
void MyNotepad::newFile(void)
{
    if (ui->textEdit->document()->isModified()) //编辑区内容是否改变
    {
        qDebug() << "document is modified";
        switch (requestIsSave())               //是否要保存
        {
            case QMessageBox::Yes :            //保存
                saveDocumentText();

                if (!currentfileName.isEmpty())
                {
                    currentfileName.clear();
                }

                setDocumentTitile("untitled -- notepad");
                ui->textEdit->document()->clear();
                break;
            case QMessageBox::No :
                setDocumentTitile("untitled -- notepad");
                ui->textEdit->document()->clear();
                break;
            case QMessageBox::Cancel :
                qDebug() << "取消";
                break;
            default:
                break;
        }
    }
    else
    {
        qDebug() << "document is not modified";
    }
}

void MyNotepad::openFile(void)
{
    if (ui->textEdit->document()->isModified()) //编辑区内容是否改变
    {
        saveDocumentText();
    }
    getDocumentText();
    setDocumentTitile(currentfileName);
}

void MyNotepad::saveFile(void)
{
    if (ui->textEdit->document()->isModified() || !currentfileName.isEmpty())
    {
        QString tmpFileName = saveDocumentText();
        setDocumentTitile(tmpFileName);
        currentfileName = tmpFileName;
    }
}

void MyNotepad::saveAsFile()
{
    if (ui->textEdit->document()->isModified())
    {
        QString tmpFileName = currentfileName;
        currentfileName.clear();
        saveDocumentText();
        currentfileName = tmpFileName;
    }
}


//退出
void MyNotepad::exitNotepad()
{
    if (ui->textEdit->document()->isModified())
    {
        switch (requestIsSave())
        {
            case QMessageBox::Yes :
                saveDocumentText();
                this->close();
                break;
            case QMessageBox::No :
                this->close();
                break;
            case QMessageBox::Cancel :
                qDebug() << "取消";
                break;
            default:
                break;
        }
    }
}

void MyNotepad::doForAboutAction()
{
//    QMessageBox::about(this, tr("关于"), tr("简单的记事本工具 - write by Ares"));
    AboutDialog about;
    about.exec();
}

void MyNotepad::selectFont()
{
    bool ok;
    QFont font = QFontDialog::getFont(&ok, this);
    if (ok)
    {
        ui->textEdit->setFont(font);
    }

}

void MyNotepad::selectColor()
{
    QColor color = QColorDialog::getColor(Qt::yellow, this, tr("颜色对话框"));
    ui->textEdit->setTextColor(color);
}

void MyNotepad::setDateTime()
{
    ui->textEdit->append(QDateTime::currentDateTime().toString());
}

void MyNotepad::seeHelp(void)
{
    QUrl url("https://me.csdn.net/qq_36413982?ref=miniprofile");
    QDesktopServices::openUrl(url);
}


QString MyNotepad::saveDocumentText(void)
{
    QString fileName;
    QFile file;
    QTextStream *out;

    if (currentfileName.isEmpty())   //如果当前没有打开的文件，就将内容保存新建的文件中，否则保存到当前打开的文件中
    {
        fileName = QFileDialog::getSaveFileName(this, tr("保存文件"), QDir::currentPath(), tr("文本(*.txt)"));
    }
    else
    {
        fileName = currentfileName;
    }
//    if (fileName.isEmpty())
//        return fileName;

    file.setFileName(fileName);
    out = new QTextStream(&file);
    if (file.open(QIODevice::WriteOnly))   //只写模式打开
    {
        *out << ui->textEdit->document()->toPlainText(); //将textEdit编辑区域内容写入文件中
        file.close();
    }

    return fileName;
}

void MyNotepad::getDocumentText()
{
    QString fileName;
    QFile file;
    QTextStream *in;

    fileName = QFileDialog::getOpenFileName(this, tr("打开文件"), QDir::currentPath(), tr("文本(*.txt)"));
    currentfileName = fileName;
    file.setFileName(fileName);
    in = new QTextStream(&file);
    if (file.open(QIODevice::ReadOnly))   //只写模式打开
    {
        ui->textEdit->setText(in->readAll());
        file.close();
    }
//    else
//    {
//        QMessageBox::warning(this, tr("提示"), file.errorString());
//    }
}

int MyNotepad::requestIsSave()
{
    int answer =  QMessageBox::question(this, tr("notepad"),
                                        tr("是否将更改保存？"), QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
    return answer;
}


void MyNotepad::setDocumentTitile(QString title)
{
    ui->textEdit->setDocumentTitle(title);
    setWindowTitle(ui->textEdit->documentTitle());
}


